package com.suitsoft.bdms.dao.account;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.suitsoft.bdms.entity.system.STreeInfo;
/**
 * 目录树管理  数据访问类.
 * @author Genius
 *
 */
@Repository
public class TreeInfoDao extends HibernateDao<STreeInfo, Long>{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 查询所有目录树信息.
	 * @return
	 */
	public List<Map<String,Object>> findAllTreeInfo(){
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT TI.NODEID,AU.ID,TI.CAPTION,DECODE(TI.PARENTID,-1,'项目总根',S.CAPTION) PANRETCAPTION,TI.NODEURL,AU.NAME,AU.NAME,TI.TARGET,DECODE(TI.NODEURL,'','1','0') ISDIR");
		sb.append(" FROM TREEINFO TI,(SELECT TI.NODEID,TI.CAPTION FROM TREEINFO TI) S,acct_AUTHORITy AU");
		sb.append(" WHERE TI.PARENTID=S.NODEID(+)");
		sb.append(" AND TI.ICON=AU.GROUP_ID(+)");
		sb.append(" ORDER BY TI.NODEID");
		return jdbcTemplate.queryForList(sb.toString());
	}
	
	/**
	 * 查询所有目录.
	 * @return
	 */
	public List<STreeInfo> findAllDir(){
		StringBuffer hql = new StringBuffer();
		hql.append(" from STreeInfo sti where sti.nodeurl is null order by sti.nodeid");
		return find(hql.toString());
	}
	
	/**
	 * 获取新的权限组ID.
	 * @return
	 */
	public long getNewGroupId(){
		return jdbcTemplate.queryForInt("select seq_tree_info_group_id.nextval from dual");
	}
	
	public void save1(Object entity){
		getSession().save(entity);
	}
	
	public void delete1(Object entiry){
		getSession().delete(entiry);
	}
	
	public int deleteRole_Authority(long id){
		return jdbcTemplate.update("DELETE acct_AUTHORITy RA WHERE RA.AUTHORITY_ID=" + id);
	}
	
	public void merge(Object entiry){
		getSession().merge(entiry);
		
	}
	
	public void flush(){
//		getSession().flush();
		getSession().clear();
	}
	
}
